#!/usr/bin/env python

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By 
from selenium.common.exceptions import StaleElementReferenceException

import tkinter as tk
from tkinter import Label, Entry, Button, messagebox, END

import constant
import time
from functools import partial

# import sys
# import io
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

def play_video(url):
    dri = Chrome()
    dri.get(url)

    dri.find_element('id', 'txtUserName').send_keys(constant.LOGIN_USERNAME)
    dri.find_element('id', 'txtPassword').send_keys(constant.LOGIN_PASSWORD)
    login_button = dri.find_element('id', 'btnLogin')
    login_button.click()
    time.sleep(2)

    bd = dri.find_element(By.XPATH, "//*[@class='bd']") \
        .find_element(By.XPATH, "//*[@class='weui-panel weui-panel_access']") \
        .find_element(By.XPATH, "//*[@class='weui-panel__bd center']") \
        .find_element(By.ID, 'video')

    while not bd.get_attribute('ended'):
        if bd.get_attribute('paused'):
            bd.click()
        time.sleep(1)

    dri.close()


def play_all(login_user, login_pawd):
    dri = Chrome()
    dri.get(constant.VIDEO_URL_HOMEPAGE)

    dri.find_element('id', 'txtUserName').send_keys(login_user.get())
    dri.find_element('id', 'txtPassword').send_keys(login_pawd.get())
    dri.find_element('id', 'btnLogin').click()
    time.sleep(3)

    videos_num = len(dri.find_element(By.XPATH, "//*[@class='video-list']").find_element(By.XPATH, "//*[@class='list']") \
            .find_element(By.ID, 'divList').find_elements(By.XPATH, '//*[@class="weui-media-box__hd video_hd"]'))
    videos = None
    for ind in range(videos_num): 
        time.sleep(1)
        # print('No.%d video finished.' % ind)
        try:
            videos = dri.find_element(By.XPATH, "//*[@class='video-list']").find_element(By.XPATH, "//*[@class='list']") \
                        .find_element(By.ID, 'divList').find_elements(By.XPATH, '//*[@class="weui-media-box__hd video_hd"]')
        except StaleElementReferenceException as e:
            # print('No.%d video play failed: %s' % (ind, e))
            continue
        videos[ind].click()
        
        # new page and play video
        bd = dri.find_element(By.XPATH, "//*[@class='bd']") \
            .find_element(By.XPATH, "//*[@class='weui-panel weui-panel_access']") \
            .find_element(By.XPATH, "//*[@class='weui-panel__bd center']") \
            .find_element(By.ID, 'video')
        while not bd.get_attribute('ended'):
            if bd.get_attribute('paused'):
                bd.click()
            time.sleep(1)

        dri.back()
    dri.close()


if __name__ == '__main__':
    window = tk.Tk()
    window.title('红十视频自动播放器')


    funny_words = Label(window,text='大喊一声赐我力量吧，可成功登录',font=("宋体",15),fg="red")
    funny_words.grid()

    xxx_word = Label(window, text='开个玩笑',font=('宋体',15),fg='green')
    xxx_word.grid()

    login_use_tip=Label(window, text='用户名',font=('宋体',15), fg='black')
    login_use_tip.grid()

    login_user = Entry(window, font=('宋体', 14), fg='black')
    login_user.grid(row=2,column=1)

    login_pawd_tip = Label(window, text='密码', font=('宋体',15), fg='black')
    login_pawd_tip.grid()

    login_pawd = Entry(window, font=('宋体', 14), fg='black')
    login_pawd.grid(row=3,column=1)


    def callback(login_user, login_pawd):
        messagebox.showinfo(title='fulu-%s-%s' % (login_user.get(), login_pawd.get()), \
                            message='视频开始播放')
        login_user.delete(0, END)
        login_pawd.delete(0, END)


    button = Button(window, text='确认播放',font=('宋体',14), fg='black', \
                    command=partial(play_all, login_user, login_pawd))
    button.grid(row=4, column=1)

    window.geometry('600x450+374+120')
    window.mainloop()
